package com.john.blog.controller.back.support;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.john.blog.common.constant.KeyConst;
import com.john.blog.common.vo.CommonResult;
import com.john.blog.model.po.Model;
import com.john.blog.service.BaseService;

/**
 * 抽象模型对象通用方法controller
 * 
 * @author JiangGengchao
 * @date 2018年2月25日
 */
public abstract class AbstractBackModelControllerSupport<T> {

    private Logger log = LoggerFactory.getLogger(AbstractBackModelControllerSupport.class);

    protected BaseService<T> service;

    @RequestMapping("/index")
    public CommonResult index(@RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize) {
        try {
            PageInfo<T> reusltPageInfo = service.findPagination(null, pageNum, pageSize);
            return CommonResult.success(reusltPageInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error("查询失败");
        }
    }

    @RequestMapping("/all")
    public CommonResult all() {
        try {
            List<T> modelList = service.selectByExample(null);
            return CommonResult.success(modelList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error("查询失败");
        }
    }

    @RequestMapping("/add")
    public CommonResult add(T model) {
        try {
            if (model instanceof Model) {
                Date date = new Date();
                ((Model) model).setCreateDate(date);
                ((Model) model).setUpdateDate(date);
            }
            service.insert(model);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error("新增失败");
        }
    }

    @RequestMapping("/del")
    public CommonResult del(String id) {
        try {
            service.deleteByPrimaryKey(id);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error("删除失败");
        }
    }

    @RequestMapping("/edit")
    public CommonResult edit(T model) {
        try {
            T result = service.updateByPrimaryKeySelective(model);
            return CommonResult.build(CommonResult.Code.SUCCESS, "修改成功", result);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error("修改失败");
        }
    }

    @RequestMapping("/get")
    public CommonResult get(String id) {
        try {
            T data = service.selectByPrimaryKey(id);
            if (null != data) {
                return CommonResult.success(data);
            } else {
                return CommonResult.error("没有查询到对象");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error("查询失败");
        }
    }

}
